package com.health.call.controller;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dyvmsapi.model.v20170525.ClickToDialRequest;
import com.aliyuncs.dyvmsapi.model.v20170525.ClickToDialResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.health.call.Pojo.AliCallLogPojo;
import com.health.call.Pojo.CLCallLogPojo;
import com.health.call.Pojo.XWCallLogPojo;
import com.health.call.service.CallLogService;
import com.health.call.service.CallService;
import com.health.call.service.impl.CallLogServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@RequestMapping("/callCenter")
@Controller
public class CallcenterController {


    @Autowired
    private CallLogService callLogService;
    @Autowired
    private CallService callService;

    @RequestMapping("/CLCallOut")
    @ResponseBody
    public void CLCallOut(String caller, String called) {

        // 1.请求API
        final JsonObject jsonObject = callService.ClCallOut("18394896115", "18561357662");

        // 2.处理返回结果
        if (jsonObject != null) {
            //响应code码 200000：成功；其他失败
            String code = jsonObject.get("code").getAsString();
            if ("200000".equals(code) && jsonObject.get("data") != null) {
                // 解析结果数据，进行业务处理
                // 校验状态码  2000000：成功，其他失败
                log.info("调用接口成功!");
                log.info("data : " + jsonObject.get("data").getAsJsonObject().toString());
            } else {
                log.info("调用接口失败...");
                log.info("code : " + code + " ; message : " + jsonObject.get("message").getAsString());
            }
        }

    }

    @RequestMapping("/ClCallLog")
    @ResponseBody
    public void ClCallLog(@RequestBody CLCallLogPojo clCallLogPojo) {
        final JsonObject jsonObject = callLogService.ClCallLog(clCallLogPojo);
        if (jsonObject != null) {
            String code = jsonObject.get("code").getAsString();
            if ("200".equals(code) && jsonObject.get("code") != null) {
                log.info("调用接口");
                log.info("data：" + jsonObject.get("data").getAsJsonObject().toString());
            } else {
                log.info("调用接口失败");
                log.info("code:" + code + "; message ;" + jsonObject.get("message").getAsJsonObject());
            }
        }
    }

    @RequestMapping("/AliCallOut")
    @ResponseBody
    public void ALiCallOut(String caller, String called) {

        DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", "LTAI5t7AVknB4aqHLwghygk5", "RAcSThYkxRMtGMBGb4JdwsB3RWJ3Fn");
        IAcsClient client = new DefaultAcsClient(profile);

        ClickToDialRequest request = new ClickToDialRequest();
        request.setCallerShowNumber("057100001121");//主叫显号
        request.setCallerNumber(caller);//呼出方
        request.setCalledShowNumber("057100001121");//被叫显号
        request.setCalledNumber(called);//被呼方
        request.setEndpoint("dyvmsapi.aliyuncs.com");
        request.setRecordFlag(true);//设置录音 true-开启 false-关闭

        try {
            ClickToDialResponse response = client.getAcsResponse(request);
            log.info("信息为" + new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            log.info("ErrCode:" + e.getErrCode());
            log.info("ErrMsg:" + e.getErrMsg());
            log.info("RequestId:" + e.getRequestId());
        }
    }

    @RequestMapping("/AliCallLog")
    @ResponseBody
    public void AliCallLog(@RequestBody AliCallLogPojo aliCallLogPojo) {
        final JsonObject jsonObject = callLogService.ALiCallLog(aliCallLogPojo);
        if (jsonObject != null) {
            String code = jsonObject.get("code").getAsString();
            if ("ok".equals(code) && jsonObject.get("data") != null) {
                log.info("调用接口成功");
                log.info("data:" + jsonObject.get("data").getAsJsonObject().toString());
            } else {
                log.info("调用接口失败");
                log.info("code:" + code + "; message ;" + jsonObject.get("message").getAsJsonObject());
            }
        }
    }

    @RequestMapping("/XWCallOut")
    public String XWCallOut() {
        return "XWcallout.html";
    }

    @RequestMapping("/XWCallLog")
    @ResponseBody
    public void XWCallLog(@RequestBody XWCallLogPojo xwCallLogPojo) {
        //1.获取通话记录
    JsonObject jsonObject  = callLogService.XWCallLog(xwCallLogPojo);
        // 2.处理返回结果
        if (jsonObject != null) {
            //响应code码 200000：成功；其他失败
            String status = jsonObject.get("status").getAsString();
            if ("200".equals(status) && jsonObject.get("data") != null) {
                // 解析结果数据，进行业务处理
                // 校验状态码  2000000：成功，其他失败
                log.info("调用接口成功!");
                log.info("data : " + jsonObject.get("data").getAsJsonObject().toString());
            } else {
                log.info("调用接口失败...");
                log.info("status: " + status + " ; message : " + jsonObject.get("message").getAsString());
            }
        }
    }

    //接口  写个判断  可以根据获取参数的不同调用不同的外呼接口
//    @RequestMapping("/")
//    public String CallConfig() {
//
//    }
//
//    public String CallLogConfig(){
//
//    }
//
//
}
